package com.dt.system.service.impl;

import java.util.List;

import com.dt.common.exception.ServiceException;
import com.dt.common.utils.DateUtils;
import com.dt.common.utils.StringUtils;
import com.dt.common.utils.bean.BeanValidators;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.dt.system.mapper.ModFileImageMapper;
import com.dt.system.domain.ModFileImage;
import com.dt.system.service.IModFileImageService;

import javax.validation.Validator;

@Service
public class ModFileImageServiceImpl implements IModFileImageService 
{
    @Autowired
    private ModFileImageMapper modFileImageMapper;

    @Autowired
    private Validator validator;

    /**
     * 查询
     * 
     * @param id 主键
     * @return
     */
    @Override
    public ModFileImage selectModFileImageById(Long id)
    {
        return modFileImageMapper.selectModFileImageById(id);
    }

    /**
     * 查询列表
     * 
     * @param modFileImage
     * @return
     */
    @Override
    public List<ModFileImage> selectModFileImageList(ModFileImage modFileImage)
    {
        return modFileImageMapper.selectModFileImageList(modFileImage);
    }

    /**
     * 新增
     * 
     * @param modFileImage
     * @return 结果
     */
    @Override
    public int insertModFileImage(ModFileImage modFileImage)
    {
        modFileImage.setCreateTime(DateUtils.getNowDate());
        return modFileImageMapper.insertModFileImage(modFileImage);
    }

    /**
     * 修改
     * 
     * @param modFileImage
     * @return 结果
     */
    @Override
    public int updateModFileImage(ModFileImage modFileImage)
    {
        modFileImage.setUpdateTime(DateUtils.getNowDate());
        return modFileImageMapper.updateModFileImage(modFileImage);
    }

    /**
     * 批量删除
     * 
     * @param ids 需要删除的主键
     * @return 结果
     */
    @Override
    public int deleteModFileImageByIds(Long[] ids)
    {
        return modFileImageMapper.deleteModFileImageByIds(ids);
    }

    /**
     * 删除信息
     * 
     * @param id 主键
     * @return 结果
     */
    @Override
    public int deleteModFileImageById(Long id)
    {
        return modFileImageMapper.deleteModFileImageById(id);
    }


    @Override
    public String importExcel(List<ModFileImage> recordsList, boolean updateSupport, String operName) {
        if (StringUtils.isNull(recordsList) || recordsList.size() == 0)
        {
            throw new ServiceException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (ModFileImage user : recordsList)
        {
            try
            {
                BeanValidators.validateWithException(validator, user);
                user.setCreateBy(operName);
                this.insertModFileImage(user);
                successNum++;
                successMsg.append("<br/>" + successNum + " 导入成功");

            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }



    /**
     * 批量新增
     * @param dataList
     * @return
     */
    public int batchInsertModFileImage(List<ModFileImage> dataList)
    {

        return modFileImageMapper.batchInsertModFileImage(dataList);
    }



}
